package be.destin.scheme.search;

import java.io.IOException;
import java.util.StringTokenizer;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import be.destin.abdera.AskosiProviderServlet;
import be.destin.skos.core.SchemeInterface;
import be.destin.skos.core.SkosManager;
import be.destin.skos.html.SkosHtmlManager;

/**
 * Servlet for Autocomplete using data from DSpace SQL database
 * 2 parameters from URL:
 *  1) index: name of the query in the configuration (autocomple.query.index-name=SQL query returning two fields: name and value
 *    SQL result "name" field is used as the effective value (to search, to store, etc.)
 *    SQL result "value" field is used as the  corresponding displayed value
 *    SQL result "count" field should provide the frequency of the value
 *  2) current: value searched that is provided as a unique parameter to the SQL query
 * 
 * @author Christophe Dupriez
 * @version $Revision: 1.22 $
 */

public class AskosiAutocompleteServlet extends AskosiServlet {

    /** Logger */
    private static Logger log = Logger.getLogger(AskosiAutocompleteServlet.class);

    static final long serialVersionUID = 1;

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        //Set encoding to UTF-8, if not set yet
        //This avoids problems of using the HttpServletRequest
        //in the getSpecialGroups() for an AuthenticationMethod,  
        //which causes the HttpServletRequest to default to 
        //non-UTF-8 encoding.
        try
        {
            if(request.getCharacterEncoding()==null)
                request.setCharacterEncoding("UTF-8");
        }
        catch(Exception e)
        {
            log.error("Unable to set encoding to UTF-8.", e);
        }
        initAskosiSchemes();
        String role = request.getParameter("index");
        String application = request.getParameter("focus");
        if (application == null || application.isEmpty()) application = request.getParameter("application");
        else {
        	int pSep = application.indexOf(SchemeInterface.separator);
        	if (pSep >= 0) {
        		role = application.substring(pSep+1);
        		application = application.substring(0,pSep);
        	}
        }
        if (application == null || application.isEmpty()) application = SkosHtmlManager.askosiDisplayApplication;
        SkosHtmlManager skosHtmlManager = new SkosHtmlManager(request, "en", skosManager, application);
    	SchemeIndexationCounter SIC = new SchemeIndexationCounter (skosHtmlManager, application, role);
    	SIC.autoComplete(request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request,response);
    }
}
